'''
Created on 09/04/2009

@author: eh01
'''
from im.ds import dsconstants, dsofactory
from im.event import eventconstants
from im.event.event import Event
from im.event.eventhandler import BaseEventHandler
from im.event.eventmanager import EventManager
from im.ds.dsstub import DSStub
from im import xmlconstants
from im.logindecorator import loginRequired
import httplib
import re

class HTTPRequestDELETENodeMessageHandler(BaseEventHandler):

    @loginRequired
    def do(self, data):
        dso = data
        http_request = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_HTTP_REQUEST).get_literal()
        lock = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_LOCK).get_literal()                    
        event_type = eventconstants.HTTP_RESPONSE_EVENT_TYPE
        
        matches = re.match('/node/(\d+)/(\d+)$', http_request.path)
        
        node_id = int(matches.group(1))
        message_id = int(matches.group(2))
                
        ds_stub = DSStub()
        ds_stub.delete_message(node_id, message_id)

        # create new dso        
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               xmlconstants.XML_CONTENT_TYPE, 
                                                               '', 
                                                               http_request, 
                                                               lock)
        event = Event(event_type, new_dso)
        EventManager().add_event(event)                